import wx
class FindDialog(wx.Dialog):
    def __init__(
            self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, 
            style=wx.DEFAULT_DIALOG_STYLE
            ):

        # Instead of calling wx.Dialog.__init__ we precreate the dialog
        # so we can set an extra style that must be set before
        # creation, and then we create the GUI dialog using the Create
        # method.
        self.textCtrl = parent
        pre = wx.PreDialog()
        pre.Create(parent, ID, title, pos, size, style)

        # This next step is the most important, it turns this Python
        # object into the real wrapper of the dialog (instead of pre)
        # as far as the wxPython extension is concerned.
        self.PostCreate(pre)

        # Now continue with the normal construction of the dialog
        # contents
        sizer = wx.BoxSizer(wx.VERTICAL)
        
        label = wx.StaticText(self, -1, "Find :")
        sizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALL, 5)
        text = self.textCtrl.GetSelectedText();
        self.text = wx.TextCtrl(self, -1, text, size=(100,-1))
        sizer.Add(self.text, 1, wx.GROW|wx.ALIGN_CENTRE|wx.ALL, 5)
        
        label = wx.StaticText(self, -1, "Replace with :")
        sizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALL, 5)
        self.replace = wx.TextCtrl(self, -1, "", size=(100,-1))
        sizer.Add(self.replace, 1, wx.GROW|wx.ALIGN_CENTRE|wx.ALL, 5)
                
        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

        btnsizer = wx.StdDialogButtonSizer()
        if wx.Platform != "__WXMSW__":
            btn = wx.ContextHelpButton(self)
            btnsizer.AddButton(btn)

        btn = wx.Button(self, wx.ID_FIND)
        btn.SetDefault()
        btnsizer.Add(btn)
        btn.Bind(wx.EVT_BUTTON, self.OnFind)

        btn = wx.Button(self, wx.ID_REPLACE,"Replace/Find")
        btnsizer.Add(btn)
        btn.Bind(wx.EVT_BUTTON, self.OnReplaceFind)

        btn = wx.Button(self, wx.ID_REPLACE,"Replace All")
        btnsizer.Add(btn)
        btn.Bind(wx.EVT_BUTTON, self.OnReplaceAll)
                

        btn = wx.Button(self, wx.ID_CANCEL)
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.text.SetFocus()
    def OnFind(self,event):
        what = self.text.GetValue()
        self.textCtrl._findNext(what)
    
    def OnReplaceFind(self,event):
        rep = self.replace.GetValue()
        what = self.text.GetValue()
        if self.textCtrl.GetSelectedText() != "":
            self.textCtrl.ReplaceSelection(rep);
        self.textCtrl._findNext(what)
    
    def OnReplaceAll(self,event):
        rep = self.replace.GetValue()
        what = self.text.GetValue()
        if what != "" :
            text = self.textCtrl.GetText()
            text = text.replace(what,rep)
            self.textCtrl.SetText(text)
